package com.rayo.accesscontrollercopy.module.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.johnkil.print.PrintView;
import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.model.db.bean.KeyData;
import com.rayo.accesscontrollercopy.utils.TimeFormatUtil;

import java.util.HashMap;


/**
 * Created by lyc-pc on 17/3/24.
 */

public class SetKeyAdapter extends RecyclerView.Adapter<SetKeyAdapter.SetKeyViewHolder> {
    private HashMap<Integer,KeyData>map;
    private Context mContext;
    private OnClicker mOnClicker;
    public SetKeyAdapter(HashMap<Integer,KeyData>map, Context context) {
        this.map=map;
        mContext = context;
    }

    @Override
    public SetKeyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SetKeyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_setkey, parent, false));
    }

    @Override
    public void onBindViewHolder(SetKeyViewHolder holder, final int position) {
        final KeyData keyData = map.get(position+1);
        holder.mTvAdapterSetkeyKeyinfoOrder.setText((position+1)+"");
        if (keyData == null) {
            holder.mPvAdapterSetkeyKeyinfoError.setVisibility(View.VISIBLE);
            holder.mTvAdapterSetkeyKeyinfoRebind.setVisibility(View.INVISIBLE);
        } else {
            holder.mPvAdapterSetkeyKeyinfoError.setVisibility(View.GONE);
            holder.mTvAdapterSetkeyKeyinfoRebind.setVisibility(View.VISIBLE);
//            if (select[position]) {
//                holder.mPvAdapterSetkeySelect.setVisibility(View.VISIBLE);
//            } else {
//                holder.mPvAdapterSetkeySelect.setVisibility(View.GONE);
//            }
//            holder.mTvAdapterSetkeyKeyinfoOrder.setText(keyData.getId() + "");
          if(keyData.getState()!=0&&keyData.getAddress()==position+1){
              holder.mTvAdapterSetkeyKeyinfoRebind.setVisibility(View.VISIBLE);
          }
            else {
              holder.mTvAdapterSetkeyKeyinfoRebind.setVisibility(View.GONE);
          }
            if(TextUtils.isEmpty(keyData.getSerialnumber())){
                holder.mTvAdapterSetkeyKeyinfoSerialnumber.setText(mContext.getString(R.string.serialnumber) + mContext.getString(R.string.unknown));
            }
            else {
                holder.mTvAdapterSetkeyKeyinfoSerialnumber.setText(mContext.getString(R.string.serialnumber) + keyData.getSerialnumber());
            }
            holder.mTvAdapterSetkeyKeyinfoTagid.setText(mContext.getString(R.string.tag_id) + keyData.getTagid());
            String time=TimeFormatUtil.showDateTime(mContext,keyData.getTime());
            if (TextUtils.isEmpty(time)){
                holder.mTvAdapterSetkeyKeyinfoTime.setText(mContext.getString(R.string.key_time) + mContext.getString(R.string.unknown));
            }
            else {
                holder.mTvAdapterSetkeyKeyinfoTime.setText(mContext.getString(R.string.key_time) + time);
            }
            if(TextUtils.isEmpty(keyData.getVersion())){
                holder.mTvAdapterSetkeyKeyinfoVersion.setText(mContext.getString(R.string.key_version) + mContext.getString(R.string.unknown));
            }
            else {
                holder.mTvAdapterSetkeyKeyinfoVersion.setText(mContext.getString(R.string.key_version) + keyData.getVersion());
            }


//             holder.mTvAdapterSetkeyKeyinfoSerialnumber.setText(mContext.getString(R.string.serialnumber)+keyData.getSerialnumber());
//             holder.mTvAdapterSetkeyKeyinfoTagid.setText(mContext.getString(R.string.tag_id)+keyData.getTagid());
//                holder.mTvAdapterSetkeyKeyinfoTime.setText(mContext.getString(R.string.key_time)+ TimeFormatUtil.showDateTime(mContext,keyData.getTime()));
//             holder.mTvAdapterSetkeyKeyinfoVersion.setText(mContext.getString(R.string.key_version)+keyData.getVersion());

        }
        holder.mTvAdapterSetkeyKeyinfoRebind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (keyData != null) {
                    mOnClicker.onClick(v, position,keyData);
                }
            }
        });
//        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                if(keyData!=null){
//                    mOnClicker.longClick(v,position);
//                }
//                return false;
//            }
//        });

    }

    @Override
    public int getItemCount() {
        return map.size();
    }

    class SetKeyViewHolder extends RecyclerView.ViewHolder {

        PrintView mPvAdapterSetkeyKeyinfoError;

        TextView mTvAdapterSetkeyKeyinfoOrder;

        TextView mTvAdapterSetkeyKeyinfoTagid;

        TextView mTvAdapterSetkeyKeyinfoRebind;

        TextView mTvAdapterSetkeyKeyinfoVersion;

        TextView mTvAdapterSetkeyKeyinfoSerialnumber;

        TextView mTvAdapterSetkeyKeyinfoTime;
        public SetKeyViewHolder(View itemView) {
            super(itemView);
           mPvAdapterSetkeyKeyinfoError=(PrintView)itemView.findViewById(R.id.pv_adapter_setkey_keyinfo_error);
            mTvAdapterSetkeyKeyinfoOrder=(TextView)itemView.findViewById(R.id.tv_adapter_setkey_keyinfo_order);
            mTvAdapterSetkeyKeyinfoTagid=(TextView)itemView.findViewById(R.id.tv_adapter_setkey_keyinfo_tagid);
            mTvAdapterSetkeyKeyinfoRebind=(TextView)itemView.findViewById(R.id.tv_adapter_setkey_keyinfo_rebind);
            mTvAdapterSetkeyKeyinfoVersion=(TextView)itemView.findViewById(R.id.tv_adapter_setkey_keyinfo_version);
            mTvAdapterSetkeyKeyinfoSerialnumber=(TextView)itemView.findViewById(R.id.tv_adapter_setkey_keyinfo_serialnumber);
            mTvAdapterSetkeyKeyinfoTime=(TextView)itemView.findViewById(R.id.tv_adapter_setkey_keyinfo_time);
        }
    }

    public void setOnClicker(OnClicker onClicker) {
        this.mOnClicker = onClicker;
    }

}
